package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.gridlayout.widget.GridLayout
import com.example.cardgame.model.CardMatchingGame

class CardGameActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var game: CardMatchingGame
    private lateinit var cardButtons: MutableList<Button>
    private lateinit var score: TextView
    private lateinit var resetButton: Button

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.third_activity)

        val gridLayout = findViewById<GridLayout>(R.id.gridLayout)
        cardButtons = mutableListOf()
        for (i in 0 until gridLayout.childCount) {
            val button = gridLayout.getChildAt(i) as Button
            button.setOnClickListener(this)
            cardButtons.add(button)
        }
        val exitButton = findViewById<Button>(R.id.button2)
        exitButton.setOnClickListener {
            exitGame()
        }

        resetButton = findViewById(R.id.button)
        resetButton.setOnClickListener { resetGame() }

        score = findViewById(R.id.textView3)
        game = CardMatchingGame(cardButtons.count())
        updateUI()
    }

    private fun resetGame() {
        game = CardMatchingGame(cardButtons.count())
        updateUI()
    }

    fun updateUI() {
        for (button in cardButtons) {
            val index = cardButtons.indexOf(button)
            if (index < game.cards.size) {
                val card = game.cardAtIndex(index)
                button.isEnabled = !card.isMatched
                if (card.isChosen) {
                    button.text = card.toString()
                    button.setTextColor(ContextCompat.getColor(this, R.color.text_color_light))
                    // 去除未翻前的背景，设置为透明或另一个背景
                    button.background = null
                    button.setBackgroundColor(Color.TRANSPARENT) // 或者使用其他合适的背景
                } else {
                    button.text = ""
                    button.setBackgroundResource(R.drawable.web_hi_res_512)
                }
            } else {
                Log.e("CardGameActivity", "Invalid card index: $index")
            }
        }
        // 更新成绩显示
        score.setTextColor(ContextCompat.getColor(this, R.color.text_color_light))
        score.text = getString(R.string.score_template, game.score)
    }

    override fun onClick(v: View) {
        if (v is Button) {
            val index = cardButtons.indexOf(v)
            game.chooseCardAtIndex(index)
            updateUI()
        }
    }
    fun exitGame() {
        // 创建 Intent 传递分数
        val resultIntent = Intent()
        resultIntent.putExtra("score", game.score)
        setResult(RESULT_OK, resultIntent)
        finish()  // This will close the current activity and return to the MainActivity
    }

}